package com.bds.btcdc.web.action;

import com.bds.btcdc.web.model.MarketData;
import com.bds.btcdc.api.model.Result;
import com.bds.btcdc.web.service.CoinService;
import com.bds.btcdc.web.service.ExchangeService;
import com.bds.btcdc.web.utils.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author: kevin
 * @date: 2018/4/28
 * @description:
 */
@Controller
public class AjaxAction {

    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    ExchangeService exchangeService;
    @Autowired
    CoinService coinService;

    /**
     * 初始化行情页面的交易所
     * @return
     */

    @RequestMapping("/exchange/init")
    @ResponseBody
    public Object exchangeInit() {
        logger.info("---------------------初始化交易所------------------------");
        List<MarketData> data = exchangeService.initExchange();
        Result rst=new Result();
        if (data!=null&&data.size()!=0){
            rst.setData(data);
            rst.setCode(Const.SUC);
        }else {
            rst.setCode(Const.FAIL);
        }
        return rst;
    }

    /**
     * 得到平台支持的交易所列表
     */
    @RequestMapping("/exchange/list")
    @ResponseBody
    public Object exchangeList() {
        List data = exchangeService.listExchange();
        Result rst=new Result();
        if (data!=null&&data.size()!=0){
            rst.setData(data);
            rst.setCode(Const.SUC);
        }else {
            rst.setCode(Const.FAIL);
        }
        return rst;
    }

    /**
     * 得到交易所下支持的币对
     */
    @RequestMapping("/exchange/pairs")
    @ResponseBody
    public Object exchangePairs(String exchange) {
        List data = exchangeService.exchangePairs(exchange);
        Result rst=new Result();
        if (data!=null&&data.size()!=0){
            rst.setData(data);
            rst.setCode(Const.SUC);
        }else {
            rst.setCode(Const.FAIL);
        }
        return rst;
    }

    /**
     * 得到交易所下支持的币
     */
    @RequestMapping("/exchange/coins")
    @ResponseBody
    public Object exchangeCoins() {
        List data = coinService.getAllUsing();
        Result rst=new Result();
        if (data!=null&&data.size()!=0){
            rst.setData(data);
            rst.setCode(Const.SUC);
        }else {
            rst.setCode(Const.FAIL);
        }
        return rst;
    }
}
